﻿namespace Yom.Data
{
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    /// <summary>
    /// 漫画数据操作
    /// 
    /// 创建: 邱柯皓 2009/01/29 12:33
    /// </summary>
    public class Comic
    {
        #region 私有成员定义
        private readonly DBHelper db = new DBHelper();
        #endregion

        #region 构造函数
        public Comic() { }
        #endregion

        #region 私有成员定义
        /// <summary>
        /// 通过DataReader获取漫画实体对象
        /// 
        /// 创建: 邱柯皓 2009/01/29 12:37
        /// </summary>
        /// <param name="dr">DataReader对象</param>
        /// <returns>返回漫画实体对象</returns>
        private Yom.Model.Comic GetModel(SqlDataReader dr)
        {
            Yom.Model.Comic model = new Yom.Model.Comic();
            model.Author = db.GetString(dr["Author"]);
            model.FaceExt = db.GetString(dr["FaceExt"]);
            model.CategoryID = db.GetInt(dr["CategoryID"]);
            model.Color = db.GetInt(dr["Color"]);
            model.ComicID = db.GetLong(dr["ComicID"]);
            model.ComicName = db.GetString(dr["ComicName"]);
            model.Initial = db.GetChar(dr["Initial"]);
            model.Intro = db.GetString(dr["Intro"]);
            model.IsCheck = db.GetBool(dr["IsCheck"]);
            model.IsTop = db.GetBool(dr["IsTop"]);
            model.Language = db.GetInt(dr["Language"]);
            model.LastTime = db.GetDateTime(dr["LastTime"]);
            model.OtherName = db.GetString(dr["OtherName"]);
            model.State = db.GetInt(dr["State"]);
            model.Tag = db.GetString(dr["Tag"]);
            model.Visit = db.GetInt(dr["Visit"]);
            model.JoinTime = db.GetDateTime(dr["JoinTime"]);

            return model;
        }

        /// <summary>
        /// 通过DataReader获取漫画列表
        /// 
        /// 创建: 邱柯皓 2009/01/29 21:47
        /// </summary>
        /// <param name="dr">DataReader对象</param>
        /// <returns>返回漫画列表</returns>
        private List<Yom.Model.Comic> GetList(SqlDataReader dr)
        {
            List<Yom.Model.Comic> list = new List<Yom.Model.Comic>();

            while (dr.Read())
                list.Add(GetModel(dr));

            return list;
        }
        #endregion

        #region 公共方法
        /// <summary>
        /// 添加漫画
        /// 
        /// 创建: 邱柯皓 2009/01/29 21:48
        /// </summary>
        /// <param name="model">漫画实体对象</param>
        /// <returns>返回新添加的漫画编号</returns>
        public long Add(Yom.Model.Comic model)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("INSERT INTO [Yom_Comic] (");
            strSQL.Append("[CategoryID], [Author], [FaceExt], [Initial], [ComicName], [OtherName], [State], ");
            strSQL.Append("[Tag], [Intro], [Language], [Color], [IsTop], [IsCheck], [Visit], [JoinTime], [LastTime]");
            strSQL.Append(") VALUES (");
            strSQL.Append("@CategoryID, @Author, @FaceExt, @Initial, @ComicName, @OtherName, @State, ");
            strSQL.Append("@Tag, @Intro, @Language, @Color, @IsTop, @IsCheck, @Visit, @JoinTime, @LastTime)");
            SqlParameter[] cmdParms = {
                db.CreateInSqlParameter("@CategoryID", DbType.Int32, model.CategoryID),
                db.CreateInSqlParameter("@Author", DbType.String, model.Author),
                db.CreateInSqlParameter("@FaceExt", DbType.String, model.FaceExt),
                db.CreateInSqlParameter("@Initial", DbType.String, model.Initial),
                db.CreateInSqlParameter("@ComicName", DbType.String, model.ComicName),
                db.CreateInSqlParameter("@OtherName", DbType.String, model.OtherName),
                db.CreateInSqlParameter("@State", DbType.Int32, model.State),
                db.CreateInSqlParameter("@Tag", DbType.String, model.Tag),
                db.CreateInSqlParameter("@Intro", DbType.String, model.Intro),
                db.CreateInSqlParameter("@Language", DbType.Int32, model.Language),
                db.CreateInSqlParameter("@Color", DbType.Int32, model.Color),
                db.CreateInSqlParameter("@IsTop", DbType.Boolean, model.IsTop),
                db.CreateInSqlParameter("@IsCheck", DbType.Boolean, model.IsCheck),
                db.CreateInSqlParameter("@Visit", DbType.Int32, model.Visit),
                db.CreateInSqlParameter("@JoinTime", DbType.DateTime, model.JoinTime),
                db.CreateInSqlParameter("@LastTime", DbType.DateTime, model.LastTime)};

            if (db.ExecuteNonQuery(CommandType.Text, strSQL.ToString(), cmdParms) > 0)
            {
                string st = "SELECT TOP 1 [ComicID] FROM [Yom_Comic] ORDER BY [ComicID] DESC";
                long val = db.GetLong(db.ExecuteScalar(CommandType.Text, st, null));
                return val;
            }

            return 0;
        }

        /// <summary>
        /// 修改漫画
        /// 
        /// 创建: 邱柯皓 2009/01/29 22:03
        /// </summary>
        /// <param name="model">漫画实体对象</param>
        /// <returns>返回成功标识</returns>
        public bool Update(Yom.Model.Comic model)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("UPDATE [Yom_Comic] SET ");
            strSQL.Append("[CategoryID]=@CategoryID, [Author]=@Author, [FaceExt]=@FaceExt, [Initial]=@Initial, ");
            strSQL.Append("[ComicName]=@ComicName, [OtherName]=@OtherName, ");
            strSQL.Append("[State]=@State, [Tag]=@Tag, [Intro]=@Intro, [Language]=@Language, [Color]=@Color, ");
            strSQL.Append("[IsTop]=@IsTop, [IsCheck]=@IsCheck, [Visit]=@Visit, ");
            strSQL.Append("[JoinTime]=@JoinTime, [LastTime]=@LastTime WHERE [ComicID]=@ComicID");
            SqlParameter[] cmdParms = {
                db.CreateInSqlParameter("@CategoryID", DbType.Int32, model.CategoryID),
                db.CreateInSqlParameter("@Author", DbType.String, model.Author),
                db.CreateInSqlParameter("@FaceExt", DbType.String, model.FaceExt),
                db.CreateInSqlParameter("@Initial", DbType.String, model.Initial),
                db.CreateInSqlParameter("@ComicName", DbType.String, model.ComicName),
                db.CreateInSqlParameter("@OtherName", DbType.String, model.OtherName),
                db.CreateInSqlParameter("@State", DbType.Int32, model.State),
                db.CreateInSqlParameter("@Tag", DbType.String, model.Tag),
                db.CreateInSqlParameter("@Intro", DbType.String, model.Intro),
                db.CreateInSqlParameter("@Language", DbType.Int32, model.Language),
                db.CreateInSqlParameter("@Color", DbType.Int32, model.Color),
                db.CreateInSqlParameter("@IsTop", DbType.Boolean, model.IsTop),
                db.CreateInSqlParameter("@IsCheck", DbType.Boolean, model.IsCheck),
                db.CreateInSqlParameter("@Visit", DbType.Int32, model.Visit),
                db.CreateInSqlParameter("@JoinTime", DbType.DateTime, model.JoinTime),
                db.CreateInSqlParameter("@LastTime", DbType.DateTime, model.LastTime),
                db.CreateInSqlParameter("@ComicID", DbType.Int64, model.ComicID)};

            return db.ExecuteNonQuery(CommandType.Text, strSQL.ToString(), cmdParms) > 0;
        }

        /// <summary>
        /// 删除漫画
        /// 
        /// 创建: 邱柯皓 2009/01/29 22:07
        /// </summary>
        /// <param name="ComicID">漫画编号</param>
        /// <returns>返回成功标识</returns>
        public bool Delete(long ComicID)
        {
            string strSQL = string.Format("DELETE FROM [Yom_Comic] WHERE [ComicID]={0}", ComicID);

            return db.ExecuteNonQuery(CommandType.Text, strSQL, null) > 0;
        }

        /// <summary>
        /// 获取漫画实体对象
        /// 
        /// 创建: 邱柯皓 2009/01/29 22:08
        /// </summary>
        /// <param name="ComicID">漫画编号</param>
        /// <returns>返回漫画实体对象</returns>
        public Yom.Model.Comic GetModel(long ComicID)
        {
            string strSQL = string.Format("SELECT * FROM [Yom_Comic] WHERE [ComicID]={0}", ComicID);

            using (SqlDataReader dr = db.ExecuteReader(CommandType.Text, strSQL, null))
            {
                Yom.Model.Comic model = new Yom.Model.Comic();

                while (dr.Read())
                    model = GetModel(dr);

                return model;
            }
        }

        /// <summary>
        /// 分页获取漫画列表
        /// 
        /// 创建: 邱柯皓 2009/01/29 22:11
        /// </summary>
        /// <param name="pageSize">每页显示数量</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <param name="condition">查询条件</param>
        /// <param name="sort">排序方式, true为正序, false为倒序</param>
        /// <returns>返回漫画列表</returns>
        public List<Yom.Model.Comic> GetList(int pageSize, int pageIndex, string condition, bool sort)
        {
            using (SqlDataReader dr = db.GetPager(pageSize, pageIndex, "[Yom_Comic]", "[ComicID]", condition, sort))
            {
                List<Yom.Model.Comic> list = GetList(dr);

                return list;
            }
        }

        /// <summary>
        /// 获取漫画列表
        /// 
        /// 创建: 邱柯皓 2009/03/09 11:24
        /// </summary>
        /// <param name="condition">查询条件</param>
        /// <returns>返回漫画列表</returns>
        public List<Yom.Model.Comic> GetList(string sql)
        {
            using (SqlDataReader dr = db.ExecuteReader(CommandType.Text, sql, null))
            {
                List<Yom.Model.Comic> list = GetList(dr);

                return list;
            }
        }

        /// <summary>
        /// 更改漫画状态
        /// 
        /// 创建: 邱柯皓 2009/01/29 22:13
        /// </summary>
        /// <param name="ComicID">漫画编号</param>
        /// <param name="State">漫画状态, 0为连载, 1为暂停, 2为完结</param>
        /// <returns>返回成功标识</returns>
        public bool SetState(long ComicID, int State)
        {
            Yom.Model.Comic model = GetModel(ComicID);
            model.State = State;

            return Update(model);
        }

        /// <summary>
        /// 修改是否推荐
        /// 
        /// 创建: 邱柯皓 2009/01/29 22:14
        /// </summary>
        /// <param name="ComicID">漫画编号</param>
        /// <param name="Top">是否推荐, true为推荐, false为不推荐</param>
        /// <returns>返回成功标识</returns>
        public bool SetTop(long ComicID, bool Top)
        {
            Yom.Model.Comic model = GetModel(ComicID);
            model.IsTop = Top;

            return Update(model);
        }

        /// <summary>
        /// 审核漫画
        /// 
        /// 创建: 邱柯皓 2009/01/29 22:16
        /// </summary>
        /// <param name="ComicID">漫画编号</param>
        /// <returns>返回成功标识</returns>
        public bool SetCheck(long ComicID)
        {
            Yom.Model.Comic model = GetModel(ComicID);
            model.IsCheck = true;

            return Update(model);
        }

        /// <summary>
        /// 增加一次阅读
        /// 
        /// 创建: 邱柯皓 2009/01/29 22:20
        /// </summary>
        /// <param name="ComicID">漫画编号</param>
        public void Visit(long ComicID)
        {
            Yom.Model.Comic model = GetModel(ComicID);
            model.Visit += 1;

            Update(model);
        }

        /// <summary>
        /// 修改漫画最后更新时间
        /// 
        /// 创建: 邱柯皓 2009/01/29 22:22
        /// </summary>
        /// <param name="ComicID">漫画编号</param>
        /// <returns>返回成功标识</returns>
        public bool SetLastTime(long ComicID)
        {
            Yom.Model.Comic model = GetModel(ComicID);
            model.LastTime = System.DateTime.Now;

            return Update(model);
        }

        /// <summary>
        /// 获取漫画数量
        /// 
        /// 创建: 邱柯皓 2009/03/06 19:43
        /// </summary>
        /// <returns>返回漫画数量</returns>
        public int GetCount()
        {
            string strSQL = "SELECT COUNT(*) FROM [Yom_Comic]";
            object m = db.ExecuteScalar(CommandType.Text, strSQL, null);
            return db.GetInt(m);
        }

        /// <summary>
        /// 通过查询条件获取数据数量
        /// 
        /// 创建: 邱柯皓 2009/03/12 16:44
        /// </summary>
        /// <param name="condition">查询条件</param>
        /// <returns>返回数据数量</returns>
        public int GetCount(string condition)
        {
            string strSQL = "SELECT COUNT(*) FROM [Yom_Comic] WHERE " + condition;
            object m = db.ExecuteScalar(CommandType.Text, strSQL, null);
            return db.GetInt(m);
        }

        /// <summary>
        /// 获取状态
        /// 
        /// 创建: 邱柯皓 2009/03/09 12:40
        /// </summary>
        /// <param name="State">状态编号</param>
        /// <returns>返回状态</returns>
        public string GetState(int State)
        {
            string val = string.Empty;
            switch (State)
            {
                case 1:
                    val = "暂停";
                    break;
                case 2:
                    val = "完成";
                    break;
                default:
                    val = "连载";
                    break;
            }

            return val;
        }

        /// <summary>
        /// 获取语言
        /// 
        /// 创建: 邱柯皓 2009/03/09 22:49
        /// </summary>
        /// <param name="Language">语言编号</param>
        /// <returns>返回语言</returns>
        public string GetLanguage(int Language)
        {
            string val = string.Empty;

            switch (Language)
            {
                case 1:
                    val = "繁体";
                    break;
                case 2:
                    val = "日语";
                    break;
                case 3:
                    val = "英语";
                    break;
                case 4:
                    val = "其他";
                    break;
                default:
                    val = "简体";
                    break;
            }

            return val;
        }

        /// <summary>
        /// 获取颜色
        /// 
        /// 创建: 邱柯皓 2009/03/09 22:52
        /// </summary>
        /// <param name="Color">颜色编号</param>
        /// <returns>返回颜色</returns>
        public string GetColor(int Color)
        {
            string val = string.Empty;
            switch (Color)
            {
                case 1:
                    val = "彩色";
                    break;
                case 2:
                    val = "未知";
                    break;
                default:
                    val = "黑白";
                    break;
            }

            return val;
        }
        #endregion
    }
}